import { Home } from '../../models/home'

const mixins = require('../../common/mixins/index')

Page({

    data: {
        message: '',
        modelList: [{
            id: 1,
            label: '童享希望',
            image: 'http://101.34.236.253/storage/student/20211106/67672518b6a5b283eedad451779769a5.png'
        }, {
            id: 2,
            label: '中奖信息',
            image: 'http://101.34.236.253/storage/student/20211106/d55395a0a854d8728d0b9e98269df0f0.png'
        }],
        serveList: [{
            id: 3,
            label: '希望币兑换',
            icon: 'http://101.34.236.253/storage/student/20211106/c76e94f55c0cfd16a1c6f4123caa6ab2.png'
        }, {
            id: 4,
            label: '留言板',
            icon: 'http://101.34.236.253/storage/student/20211106/6adba143db9fda486074a4174c3d4d04.png'
        }, {
            id: 5,
            label: '联系我们',
            icon: 'http://101.34.236.253/storage/student/20211106/17e7bc6ca2397e8b9b1b5868a33d90ee.png'
        }],

        showNotifyWindow: false,
        showAccountWindow: false,
        showMessageWindow: false,
        showCallWindow: false,
        delay: false
    },

    async onLoad () {
        this.setNavBarHeight()
        await this.getData()
    },

    async getData () {
        const info = await Home.getHomeData()

        this.setData({
            info,
            showNotifyWindow: !!info.tip_notice
        })
    },

    async onSubmitMessage () {
        const message = this.data.message
        if (!message || message === '' || message.trim() === '') {
            return
        }
        const status = await Home.onMessage(message)
        wx.showToast({
            title: status === 200 ? '提交成功' : '提交失败',
            icon: status === 200 ? 'success' : 'error'
        })
        this.setData({
            showMessageWindow: false,
            message: ''
        })
    },

    onTapModel (e) {
        const id = e.currentTarget.dataset.id
        let url = ''
        switch (+id) {
        case 1: // 童享希望
            url = '/pages/hope/hope'
            break
        case 2: // 中奖信息
            url = '/pages/award/award'
            break
        case 3: // 希望币兑换
            url = '/pages/exchange/exchange'
            break
        case 4: // 留言板
            this.setData({
                showMessageWindow: true
            }, () => { // 处理输入框和弹窗兼容bug（弹窗时占位符宽度不够）
                setTimeout(() => {
                    this.setData({
                        delay: true
                    })
                }, 300)
            })
            break
        case 5: // 联系我们
            this.setData({showCallWindow: true})
            break
        default:
            throw Error(`unknown id: ${id}`)
        }
        if (url === '') {
            return
        }
        wx.navigateTo({url})
    },

    handleInput (e) {
        this.setData({
            message: e.detail.value
        })
    },

    onCall (e) {
        mixins.onCall(e.currentTarget.dataset.number)
    },

    async onOpenWindow (e) {
        const {field} = e.currentTarget.dataset
        if (field === 'showAccountWindow') {
            this.setData({
                [`${field}`]: true,
                account: await Home.getUserAccount()
            })
            return
        }
        this.setData({
            [`${field}`]: true
        })
    },

    onCloseWindow (e) {
        const {field} = e.currentTarget.dataset
        this.setData({
            [`${field}`]: false
        }, () => { // 处理输入框和弹窗兼容bug（弹窗时占位符宽度不够）
            setTimeout(() => {
                this.setData({
                    delay: false
                })
            }, 300)
        })
    },

    setNavBarHeight () {
        const menu = wx.getMenuButtonBoundingClientRect()
        this.setData({
            statusBarHeight: menu.height + menu.top + 26
        })
    }
})
